﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HTTT.Utilities;
using HTTT.Bussiness;
using HTTT.DataAccess;

namespace HTTT.admin.htdispatch
{
	public partial class DispatchList : System.Web.UI.Page
	{
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["keyword"] != null)
                {
                    txtKeyword.Text = Session["keyword"].ToString();
                }
                CheckPermission();
                LoadData();
            }
        }

        private void CheckPermission()
        {
            if (!Page.User.IsInRole(Constants.ROLE_SUPER_ADMINISTRATOR)
                 && !Page.User.IsInRole(Constants.ROLE_ADMINISTRATOR))
            {
                Response.Redirect("/admin/AccessDenied.aspx");
            }
        }

        private void LoadData()
        {
            HT_DispatchBO BO = new HT_DispatchBO();
            IEnumerable<HT_Dispatch> Data;
            string keyword = txtKeyword.Text.Trim();
            if (keyword == string.Empty)
            {
                Data = BO.GetAllHT_Dispatch();
            }
            else
            {
                Data = BO.SearchHT_Dispatch(keyword);
            }
            if(Data != null)
            {
                rptData.DataSource = Data.ToList();
                rptData.DataBind();
            }
        }
        protected void btnSearch_Click(Object sender, EventArgs e)
        {
            Session["keyword"] = txtKeyword.Text.Trim();
            LoadData();
        }

        protected void btnClear_Click(Object sender, EventArgs e)
        {
            txtKeyword.Text = "";
            Session["keyword"] = txtKeyword.Text.Trim();
            LoadData();
        }
        protected void btnAdd_Click(Object sender, EventArgs e)
        {
            Response.Redirect("AddDispatch.aspx");
        }

        protected void btnEdit_Click(Object sender, EventArgs e)
        {
            Guid ID = GetSelectedID();
            if (ID != null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg("Phải chọn 1 công văn yêu cầu"), true);
                return;
            }
            Response.Redirect("EditDispatch.aspx?ID=" + ID);
        }
        protected void btnDelete_Click(Object sender, EventArgs e)
        {
            Guid ID = GetSelectedID();
            if (ID != null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg("Phải chọn 1 công văn yêu cầu"), true);
                return;
            }
            if (ID != null)
            {
                HT_DispatchBO BO = new HT_DispatchBO();
                BO.DeleteHT_Dispatch(ID);
            }
        }
        
        private Guid GetSelectedID()
        {
            string SelectedID = "";
            Guid ret;
            foreach (RepeaterItem ri in rptData.Items)
            {
                CheckBox chk = (CheckBox)ri.FindControl("chkItem");
                if (chk.Checked)
                {
                    SelectedID = chk.Attributes["value"];
                }
            }
            bool valid = Guid.TryParse(SelectedID, out ret);
            
            return ret;
        }
	}
}